package com.jeesite.modules.mo.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.mo.entity.MoRecharge;
import com.jeesite.modules.mo.service.MoRechargeService;

/**
 * 充值Controller
 * @author 李寒冰
 * @version 2025-02-18
 */
@Controller
@RequestMapping(value = "${adminPath}/mo/moRecharge")
public class MoRechargeController extends BaseController {

	@Autowired
	private MoRechargeService moRechargeService;
    @Autowired
    private IcUserService icUserService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public MoRecharge get(String id, boolean isNewRecord) {
		return moRechargeService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("mo:moRecharge:view")
	@RequestMapping(value = {"list", ""})
	public String list(MoRecharge moRecharge, Model model) {
		model.addAttribute("moRecharge", moRecharge);
		return "modules/mo/moRechargeList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("mo:moRecharge:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<MoRecharge> listData(MoRecharge moRecharge, HttpServletRequest request, HttpServletResponse response) {
		moRecharge.setPage(new Page<>(request, response));
		Page<MoRecharge> page = moRechargeService.findPage(moRecharge);
		List<MoRecharge> list = page.getList();
		for (MoRecharge item : list) {
			String userId = item.getUserId();
			IcUser icUser = icUserService.get(userId);
			item.setIcUser(icUser);
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("mo:moRecharge:view")
	@RequestMapping(value = "form")
	public String form(MoRecharge moRecharge, Model model) {
		model.addAttribute("moRecharge", moRecharge);
		return "modules/mo/moRechargeForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("mo:moRecharge:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated MoRecharge moRecharge) {
		moRechargeService.save(moRecharge);
		return renderResult(Global.TRUE, text("保存充值成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("mo:moRecharge:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(MoRecharge moRecharge) {
		moRechargeService.delete(moRecharge);
		return renderResult(Global.TRUE, text("删除充值成功！"));
	}
	
}